import { defineStore } from "pinia"
import { ref } from "vue"
import { getRouters, getInfo } from '@/api'

export default defineStore('', _ => {
    // token相关
    let token = ref(localStorage.getItem('token') || null)
    let setToken = v => {
        token.value = v
        localStorage.setItem('token', v)
    }

    let userInfo = ref({})
    let setUserInfo = v => {
        userInfo.value = v
    }
    let getUserInfo = async _ => {
        let r = await getInfo()
        userInfo.value = r.data
    }

    // 是否注册过动态路由
    let isRegMenu = ref(false)
    let setIsRegMenu = v => {
        isRegMenu.value = v
    }
    // 动态路由（菜单）数据
    let routerMenu = ref([])
    let setRouterMenu = v => {
        routerMenu.value = v
    }
    // 获取动态路由（菜单）
    let getRouterMenu = async _ => {
        let r = await getRouters()
        routerMenu.value = r
        return r
    }
    return {
        token, setToken,
        userInfo, setUserInfo, getUserInfo,
        isRegMenu, setIsRegMenu,
        routerMenu, setRouterMenu, getRouterMenu
    }
})